#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

#include "binfile.h"
#include "both.h"


Int64 fCount, fCountPre, fHalfMask;
Int8 fLen, fLenPre, n, nPre;
vector<Int8> CompArray;
vector<bool> MarkArray;

BinFile writer, reader;

inline void ExtractClass(const Int64 f) {
  Int64 fTmp2 = f;
  for(Int8 j = 0; j < Gray.size(); j++) {
    if (j != 0) {
      fTmp2 = negbyvar(fTmp2, Gray[j], nPre);
    }
    CompArray[fTmp2] = CompArray[f];
  }
}

inline void MarkClassBy(const Int64 f) {
  Int64 fTmp2 = f;
  for(Int8 j = 0; j < Gray.size(); j++) {
    if (j != 0) {
      fTmp2 = negbyvar(fTmp2, Gray[j], n);
    }
    MarkArray[fTmp2] = true;
  }    
}


inline void LoadInfo() {  
  ostringstream oss;
  nPre = n - 1;
  fLen = 1 << n;
  fLenPre = 1 << nPre;
  fCount = (Int64)1 << fLen;
  fCountPre = (Int64)1 << fLenPre;
  MarkArray.resize(fCount);
  CompArray.resize(fCountPre);
  
  fHalfMask = ((Int64)1 << (fLen >> 1)) - 1;  
  
  oss << "binfile_nbv" << (int)nPre << "v.mbf";  
  reader.open(oss.str(), BIN_FILE_READ);
  
  MakeAddArrays(nPre);
  
  for(Int32 f = 0; f < fCountPre; f++)
    if (CompArray[f] == 0) {
      Int32 tf;
      reader.read(tf);
      reader.read(CompArray[tf]);
      cout << tf << ' ' << f << endl;      
      ExtractClass(tf);
    }
  
  oss.str("");
  oss << "binfile_nbv" << (int)n << "v.mbf"; 
  writer.open(oss.str(), BIN_FILE_WRITE);
  
  MakeAddArrays(n); 
  
  cout << "Writing!" << endl;  
}

inline void SaveInfo(const Int64 f, const Int8 fComp) {
  writer.write((Int32)f);
  writer.write(fComp);
}

#include "gen1_5.h"

int main () {
  n = 5;
  LoadInfo();
  
  Int8 fComp;

  for(Int64 f = 0; f < fCount; f++)
    if (NotMarked(f)) {
      FindComplexityOfTo(f,fComp);
      MarkClassBy(f);
      SaveInfo(f,fComp);        
    }

  return 0;
}
